package com.sendpo.zjx.sendpo.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sendpo.zjx.sendpo.R;
import com.sendpo.zjx.sendpo.fragment.helpingorder.BaseHelpingFragment;
import com.sendpo.zjx.sendpo.fragment.helpingorder.MyselfHelpingorderFragment;
import com.sendpo.zjx.sendpo.fragment.helpingorder.OtherHelpingorderFragment;
import com.sendpo.zjx.sendpo.utils.Tools;
import com.shizhefei.view.indicator.IndicatorViewPager;
import com.shizhefei.view.indicator.ScrollIndicatorView;
import com.shizhefei.view.indicator.slidebar.ColorBar;
import com.shizhefei.view.indicator.transition.OnTransitionTextListener;
import com.shizhefei.view.viewpager.SViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/8/21.
 */

public class HelpingOrderListActivity extends AppCompatActivity {

    @BindView(R.id.backbt)
    RelativeLayout backbt;
    @BindView(R.id.indicator)
    ScrollIndicatorView indicator;
    @BindView(R.id.viewpager)
    SViewPager viewpager;
    private IndicatorViewPager indicatorViewPager;
   public static  final String[] titles=new String[]{"发布","接单"};
    private MyselfHelpingorderFragment myself;
    private OtherHelpingorderFragment other;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_helpingorderlist);
        ButterKnife.bind(this);
        Tools.initStatus(this, R.color.black);
        initComponent();
    }

    public void initComponent() {
        viewpager.setCanScroll(false);
        viewpager.setOffscreenPageLimit(1);
        indicatorViewPager = new IndicatorViewPager(indicator, viewpager);
        List<BaseHelpingFragment> fragments = new ArrayList<>();
        fragments.add(myself=new MyselfHelpingorderFragment());
        fragments.add(other=new OtherHelpingorderFragment());
        indicatorViewPager.setAdapter(new FragmentIndicatorAdapter(getSupportFragmentManager(), fragments));
        indicatorViewPager.setClickIndicatorAnim(true);
        //点击切换颜色
        indicatorViewPager.setIndicatorOnTransitionListener(new OnTransitionTextListener().setColor(getResources().getColor(R.color.default_theme), getResources().getColor(R.color.default_gray)));
        //颜色块
        ColorBar colorBar = new ColorBar(this, getResources().getColor(R.color.default_theme), 5);
        colorBar.setWidth(120);
        indicatorViewPager.setIndicatorScrollBar(colorBar);
        indicatorViewPager.setOnIndicatorPageChangeListener(new IndicatorViewPager.OnIndicatorPageChangeListener() {
            @Override
            public void onIndicatorPageChange(int preItem, int currentItem) {
                   switch (currentItem){
                       case 0:
                        if(myself!=null){
                            if(myself.pulltofresh!=null){
                         myself.pulltofresh.autoRefresh();
                            }
                        }
                           break;
                       case 1:
                           if(other!=null){
                               if(other.pulltofresh!=null){
                                   other.pulltofresh.autoRefresh();
                               }
                           }
                           break;

                   }
            }
        });

    }



    private class FragmentIndicatorAdapter extends IndicatorViewPager.IndicatorFragmentPagerAdapter {
        private List<BaseHelpingFragment> fragments;

        public FragmentIndicatorAdapter(FragmentManager fragmentManager, List<BaseHelpingFragment> fragments) {
            super(fragmentManager);
            this.fragments = fragments;
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public View getViewForTab(int position, View convertView, ViewGroup container) {
            if (convertView == null) {
                convertView = View.inflate(HelpingOrderListActivity.this, R.layout.tab_focus, null);
            }
            TextView textView = (TextView) convertView;
            textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            textView.setText(titles[position]);
            return textView;
        }

        @Override
        public Fragment getFragmentForPage(int position) {
            return fragments.get(position);
        }
    }
    @OnClick(value = {R.id.backbt})
    public void onMyClick(View view) {
        switch (view.getId()) {
            case R.id.backbt:
                finish();

                break;

        }
    }
}
